package vn.bus.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import vn.bus.dto.NearbyConnectionDTO;

public class NearbyConnectionDAO {

	public ArrayList<NearbyConnectionDTO> getAllNearbyConnections(Connection connection) throws Exception
	{
		ArrayList<NearbyConnectionDTO> allNearbyConns = new ArrayList<NearbyConnectionDTO>();
		try
		{
			PreparedStatement ps = connection.prepareStatement("SELECT * FROM nearby_connections");
			ResultSet rs = ps.executeQuery();
			while(rs.next())
			{
				NearbyConnectionDTO NearbyConn = new NearbyConnectionDTO();
				NearbyConn.setDistance(rs.getInt("distance"));
				NearbyConn.setId(rs.getInt("id"));
				NearbyConn.setNearby(rs.getString("nearby"));
				NearbyConn.setStation(rs.getString("station"));
				NearbyConn.setPolyline(rs.getString("polyline"));
				allNearbyConns.add(NearbyConn);
			}
			return allNearbyConns;
		}
		catch(Exception e)
		{
			throw e;
		}
	}
	
	public String updatePolyline(Connection connection, String polyline, String station, String nearby) 
			throws Exception{
		String rs = "";
		try
		{
			String rq = "UPDATE nearby_connections SET `polyline`=\"" + polyline + "\" WHERE `station`=\"" + station
					+ "\" AND `nearby`=\"" + nearby + "\"";
			PreparedStatement ps = connection.prepareStatement(rq);
			ps.executeUpdate();
			rs = "OK";
			System.out.println("UpdateNearbyPolyline : " + polyline + " -> successful");
		}
		catch(Exception e)
		{
			throw e;
		}
		return rs;
	}
}
